/* ****************************************************************************
 *
 * Copyright (c) Microsoft Corporation. 
 *
 * This source code is subject to terms and conditions of the Microsoft Public License. A 
 * copy of the license can be found in the License.html file at the root of this distribution. If 
 * you cannot locate the  Microsoft Public License, please send an email to 
 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound 
 * by the terms of the Microsoft Public License.
 *
 * You must not remove this notice, or any other, from this software.
 *
 * THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
 * PARTICULAR PURPOSE.
 * ***************************************************************************/


#include "stdafx.h"
#include <pgl/core/pgl.h>
#include <pgl/core/PGLLine2DLODPropPage.h>
#include <pgl/core/PGLRegion.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPGLLine2DLODPropPage property page

IMPLEMENT_DYNCREATE(CPGLLine2DLODPropPage, CPropertyPage)

CPGLLine2DLODPropPage::CPGLLine2DLODPropPage(CPGLLine2DLOD* _pLine2DLOD) : CPropertyPage(CPGLLine2DLODPropPage::IDD)
{
	//{{AFX_DATA_INIT(CPGLLine2DLODPropPage)
	m_iNbPApprox = 0;
	m_iNbPOrig = 0;
	m_dTol = 0.0;
	m_dPreview = FALSE;
	//}}AFX_DATA_INIT

	m_pLine2DLOD=_pLine2DLOD;
}

CPGLLine2DLODPropPage::~CPGLLine2DLODPropPage()
{
}

void CPGLLine2DLODPropPage::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPGLLine2DLODPropPage)
	DDX_Text(pDX, IDC_PGL_EDIT_NBPAPPROX, m_iNbPApprox);
	DDX_Text(pDX, IDC_PGL_EDIT_NBPORIG, m_iNbPOrig);
	DDX_Text(pDX, IDC_PGL_EDIT_TOL, m_dTol);
	DDX_Check(pDX, IDC_PGL_CHECK_PREVIEW, m_dPreview);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPGLLine2DLODPropPage, CPropertyPage)
	//{{AFX_MSG_MAP(CPGLLine2DLODPropPage)
	ON_EN_CHANGE(IDC_PGL_EDIT_TOL, OnChangePglEditTol)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPGLLine2DLODPropPage message handlers

BOOL CPGLLine2DLODPropPage::OnKillActive() 
{
	using namespace hull;
	UpdateData(TRUE);

	CPGLLine2DLOD::LineApprox& hull=m_pLine2DLOD->GetHull();

	// Setting tolerance
	hull.SetTol(static_cast<float>(m_dTol));

	// Simplify
	hull.Simplify();	
	
	return CPropertyPage::OnKillActive();
}

void CPGLLine2DLODPropPage::OnChangePglEditTol() 
{
	UpdateData(TRUE);
	if (m_dPreview)
	{
		CPGLLine2DLOD::LineApprox& hull=m_pLine2DLOD->GetHull();

		// Setting tolerance
		hull.SetTol(static_cast<float>(m_dTol));
	
		hull.Simplify();
		Refresh();
	}

	UpdateData(FALSE);
}

void CPGLLine2DLODPropPage::Refresh()
{
	CPGLLine2DLOD::LineApprox& hull=m_pLine2DLOD->GetHull();

	m_iNbPOrig=hull.GetPointSize();
	m_iNbPApprox=hull.GetKeySize();
	m_dTol=hull.GetTol();
}

BOOL CPGLLine2DLODPropPage::OnSetActive() 
{
	ASSERT(m_pLine2DLOD);
	m_dPreview=TRUE;

	Refresh();
	UpdateData(FALSE);
	
	return CPropertyPage::OnSetActive();
}
